import os
import re
import time
from urllib.parse import urlparse

import requests

url = 'https://www.hdbz.net/'
res = requests.get(url)
html_content = res.content.decode()
rs = re.findall(r'<img src="/templets/images/gray.gif" data-original="(.*?)" (.*?)/>', html_content)

count = 1
for img_url in rs:
    parsed_url = urlparse(img_url[0].split('?')[0])

    file_name = os.path.basename(parsed_url.path)
    img_type = os.path.splitext(file_name)[1]

    # 高清图
    img_res = requests.get(img_url[0].split('?')[0])
    # 小图
    # img_res = requests.get(img_url[0])

    print(f'正在爬取第{count}张图片')
    with open(f'图片/img{count}{img_type}', 'wb') as file:
        file.write(img_res.content)
        count += 1
        time.sleep(1)
